from flask import request, g
from utils.jwt_util import verify_jwt


def get_userinfo():
    """获取用户信息"""
    # 获取请求头中的token
    token = request.headers.get('Authorization')

    g.userid = None  # 如果未登录, userid=None
    g.is_refresh = None
    if token:  # 如果传递了token
        # 校验token
        data = verify_jwt(token)

        if data:  # 校验成功
            g.userid = data.get('userid')  # 如果已登录, userid=11
            g.is_refresh = data.get('is_refresh')